﻿/*****************************************************
 * 本类库的核心系 JNTemplate
 * (C) JNTemplate 2011
 * 作者：翅膀的初衷 QQ:4585839
 * 网址：http://www.JiNianNet.com
 *****************************************************/

using System;
using System.Collections.Generic;

namespace JinianNet.JNTemplate.Process.Element
{
    public class LoadTag : SimpleTag
    {
        public LoadTag(string text)
            : base(TokenKind.Load, text)
        {

        }

        private string _basePath;
        public string BasePath
        {
            get { return _basePath; }
            set { _basePath = value; }
        }

        private string _path;
        public string Path
        {
            get { return _path; }
            set { _path = value; }
        }

        public override object Parser(VariableScope varScope)
        {
            using (System.IO.StringWriter write = new System.IO.StringWriter())
            {
                Parser(varScope, write);
                return write.ToString();
            }
        }

        public override void Parser(VariableScope varScope, System.IO.TextWriter write)
        {
            List<object> args = new List<object>();

            if (!string.IsNullOrEmpty(this._path))
            {
                this._path = _path.Trim();
                if (this._path.Length > 0)
                {
                    args =new TemplateParser().Split(this._path, '+');
                }
            }

            for (int i = 0; i < args.Count; i++)
            {
                #region
                if (args[i] is SimpleTag)
                {

                    args[i] = (((Token)args[i]).Parser(varScope));
                }
                #endregion
            }

            ITemplate template;
            if (!string.IsNullOrEmpty(BasePath))
            {
                template = BuildManager.CreateTemplate(Common.FileHelper.ToRealPath(string.Concat(args.ToArray()), BasePath));
            }
            else
            {
                template = BuildManager.CreateTemplate(string.Concat(args.ToArray()));
            }
            template.Context.TempData = varScope;
            template.Render(write);
            //(new Template(Common.FileHelper.ToRealPath(string.Concat(args.ToArray()),_basePath), varScope)).Render(write);
        }
    }


}
